package db.tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.db.BeanBase;
import com.db.Table;

import db.beans.T_TranslateBean;


public class T_TranslateTable extends Table{
	
	public static final String COL_KEY_WORLD_NO = "KEY_WORLD_NO";
	public static final String COL_SUB_NO = "SUB_NO";
	public static final String COL_DESTINATION = "DESTINATION";
	public static final String COL_ORDER = "ORDER";
	public static final String COL_DELETE_FLAG = "DELETE_FLAG";
	
	
	public static BeanBase[] selectAll(Connection conn, String strTableName) throws SQLException{
		final String sql = "SELECT " 
			+ COL_KEY_WORLD_NO + ","
			+ COL_SUB_NO + "," 
			+ COL_DESTINATION + ","
			+ COL_ORDER + ","
			+ COL_DELETE_FLAG
			+ " FROM " + strTableName;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<T_TranslateBean> v = new Vector<T_TranslateBean>();
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int i = 1;
			while (rs.next()) {
				i = 1;
				T_TranslateBean rec = new T_TranslateBean();
				rec.setKeyWorldNo(rs.getInt(i++));
				rec.setSubNo(rs.getInt(i++));
				rec.setSource(rs.getString(i++));
				rec.setDestination(rs.getString(i++));
				rec.setOrder(rs.getInt(i++));
				rec.setDeleteFlag(rs.getInt(i++) == NOT_DELETE_FLAG);
				v.add(rec);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}
	
			if (stmt != null) {
				stmt.close();
			}
		}
		return (T_TranslateBean[]) v.toArray(new T_TranslateBean[v.size()]);
	}
	
	public static Map<String, String[]> selectAll(Connection conn, int topCount, String strTableName) throws SQLException{
		final String sql = "SELECT " 
			+ COL_KEY_WORLD_NO + ","
			+ COL_SUB_NO + "," 
			+ COL_DESTINATION + ","
			+ COL_ORDER + ","
			+ COL_DELETE_FLAG
			+ " FROM " + strTableName + ","
			+ "Ranking"
			+ " WHERE " + strTableName + "." + COL_KEY_WORLD_NO + "=" 
			
			+ " ORDER BY " + COL_KEY_WORLD_NO + "," + COL_ORDER;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Map<String, String[]> mapTranslate = new HashMap<String, String[]>();
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int oldKeyWordNo = -1;
			int curKeyWordNo = -1;
			String oldKeyWorld = "";
			Vector<String> v = new Vector<String>();
			while (rs.next()) {
				curKeyWordNo = rs.getInt(COL_KEY_WORLD_NO);
				
				if (oldKeyWordNo != -1 && oldKeyWordNo != curKeyWordNo){
					mapTranslate.put(oldKeyWorld, (String[])v.toArray(new String[v.size()]));
					v.clear();
				}
				v.add(rs.getString(COL_DESTINATION));
				
				oldKeyWordNo = curKeyWordNo;
				oldKeyWorld = rs.getString(COL_KEY_WORLD_NO);
			}
			
		} finally {
			if (rs != null) {
				rs.close();
			}
	
			if (stmt != null) {
				stmt.close();
			}
		}
		return mapTranslate;
	}
}
